/**
 * 
 */
package soa.security.test.ws;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.junit.Test;

/**
 * @author Cre.Gu
 * 
 */
public class GroupServiceClient extends WebServiceClient {
	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(GroupServiceClient.class);

	public static final String WSURL = BASE_WSDL + "GroupService?wsdl";

	public Boolean addGroup(String organizationId, String group) {
		try {
			String method = "addGroup";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("group", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organizationId, group });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public Boolean updateGroup(String organizationId, String group) {
		try {
			String method = "updateGroup";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("group", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organizationId, group });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String allGroups(String organizationId) {
		try {
			String method = "allGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			Object obj = call.invoke(new Object[] { organizationId });
			String retData = (String) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getGroup(String organizationId, String groupId) {
		try {
			String method = "getGroup";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call
					.invoke(new Object[] { organizationId, groupId });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Boolean addPersons(String organizationId, String groupId,
			String personIds) {
		try {
			String method = "addPersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("personIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					groupId, personIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean removePersons(String organizationId, String groupId,
			String personIds) {
		try {
			String method = "removePersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("personIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					groupId, personIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allPersons(String organizationId, String groupId) {
		try {
			String method = "allPersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call
					.invoke(new Object[] { organizationId, groupId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean addSubGroups(String organizationId, String groupId,
			String subGroupIds) {
		try {
			String method = "addSubGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("subGroupIds", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					groupId, subGroupIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean removeSubGroups(String organizationId, String groupId,
			String subGroupIds) {
		try {
			String method = "removeSubGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("subGroupIds", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					groupId, subGroupIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allSubGroups(String organizationId, String groupId) {
		try {
			String method = "allSubGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupId", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call
					.invoke(new Object[] { organizationId, groupId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	@Test
	public void testAdd() throws Exception {
		addGroup(ORGANIZATIONID, WebServiceClient.readXml("group.xml"));
	}

	@Test
	public void testUpdate() throws Exception {
		updateGroup(ORGANIZATIONID, WebServiceClient.readXml("group.xml"));
	}

	@Test
	public void testOther() throws Exception {

		String all = allGroups(ORGANIZATIONID);
		System.out.println(all);

		String get = getGroup(ORGANIZATIONID, "GR0001");
		System.out.println(get);
	}

	@Test
	public void testRel() throws Exception {
		addPersons(ORGANIZATIONID, "GR0001", "PE0001,PE0002");
		System.out.println(allPersons(ORGANIZATIONID, "GR0001"));

		removePersons(ORGANIZATIONID, "GR0001", "PE0001");
		System.out.println(allPersons(ORGANIZATIONID, "GR0001"));
	}

	@Test
	public void testRel2() throws Exception {
		addSubGroups(ORGANIZATIONID, "GR0001", "GR0002");
		System.out.println(allSubGroups(ORGANIZATIONID, "GR0001"));

		removeSubGroups(ORGANIZATIONID, "GR0001", "GR0002");
		System.out.println(allSubGroups(ORGANIZATIONID, "GR0001"));
	}
}
